<?php
/**
 * Course Class Model
 *
 * Copyright 2012
 * @date 21-April-2012
 * @author 
 * @version 0.0.1
 **/
class SurveyQuestion extends AppModel {
/**
 * Name
 *
 * @var string
 */
    public $name = 'SurveyQuestion';
    public $useTable = "surveys_questions";
    public $belongsTo = array(
        'Survey'=>array(
            'className'=>'Survey',
            'foreignKey'=>'surveys_id'
        ),
        'Question'=>array(
            'className'=>'Question',
            'foreignKey'=>'questions_id'
        )
    );
    public function save_position($survey_id , $question_id,$position){
        $this->query("update surveys_questions set position=$position where surveys_id=$survey_id AND questions_id=$question_id");
    }
    public function get_question_by_survey($customer_id) {
        $this->unbindModel(array("belongsTo"=>array("Survey")));
        return $this->find("all",array('conditions'=>array('surveys_id'=>$customer_id),'order'=>array("position asc")));
    }
    public static function count_question_by_survey($customer_id) {
        $sq = ClassRegistry::init("SurveyQuestion");
        $sq->unbindModel(array("belongsTo"=>array("Survey")));
        return $sq->find("count",array('conditions'=>array('surveys_id'=>$customer_id)));
    }
    public function delete_question_by_survey($customer_id) {
        return $this->query("Delete from surveys_questions where surveys_id=$customer_id");
    }
    public function delele_survey_by_question($group_id) {
        return $this->query("Delete from surveys_questions where questions_id=$group_id");
    }
    public function delete_survey_question($surveys_id,$question_id) {
        return $this->query("Delete from surveys_questions where surveys_id=$surveys_id AND questions_id=$question_id");
    }
    public static function check_question($surveys_id,$question_id){
        $sq = ClassRegistry::init("SurveyQuestion");
        $check = $sq->find("all",array('conditions'=>array('surveys_id'=>$surveys_id,'questions_id'=>$question_id)));
        if(!empty($check)){
            return 1;
        }
        return 0;
    }
}